﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CrmDocman.DataCore;

/// <summary>
/// Summary description for DoiTacHelper
/// </summary>
public class DoiTacHelper : TWSData
{
    public DoiTacHelper()
    {
        m_dao = DataBridge.GetInstance();
    }

    public DoiTacDataSet LoadDoiTac()
    {

        DoiTacDataSet lspDs = new DoiTacDataSet();
        string sql = m_dao.BuildSelectQuery(lspDs.DoiTac);
        sql += " order by ThuTu ";

        m_dao.FillDataTable(lspDs.DoiTac, sql);
        m_dao.Connection.CloseConnection();
        return lspDs;
    }

    public DoiTacDataSet LoadDoiTac(int DoiTacID)
    {

        DoiTacDataSet lspDs = new DoiTacDataSet();
        string sql = m_dao.BuildSelectQuery(lspDs.DoiTac);
        sql += " where ID = " + DoiTacID;

        m_dao.FillDataTable(lspDs.DoiTac, sql);
        m_dao.Connection.CloseConnection();
        return lspDs;
    }

    public bool SaveDoiTac(DoiTacDataSet lspDs)
    {
        m_dao.UpdateDataTable(lspDs.DoiTac);
        m_dao.Connection.CloseConnection();
        return true;

    }

    public DoiTacDataSet LoadDoiTac(int page, int pageSize, string sortColumn, string sortOrder)
    {

        DoiTacDataSet spDs = new DoiTacDataSet();
        string sql = "DECLARE @PageNum AS INT; ";
        sql += "DECLARE @PageSize AS INT; ";
        sql += "SET @PageNum = " + page + "; ";
        sql += "SET @PageSize = " + pageSize + "; ";
        sql += "WITH OrdersRN AS ( ";
        sql += "SELECT ROW_NUMBER() OVER(ORDER BY " + sortColumn + " " + sortOrder + ") AS RowNum, * ";
        sql += "FROM dbo.DoiTac  ) ";
        sql += "select * from OrdersRN ";
        sql += " WHERE RowNum BETWEEN (@PageNum - 1) * @PageSize + 1 AND @PageNum * @PageSize ";
        sql += " order by " + sortColumn + " " + sortOrder;

        m_dao.FillDataTable(spDs.DoiTac, sql);
        m_dao.Connection.CloseConnection();
        return spDs;
    }
}